/*
 * 文件名：BaseSelectOptionSearchContrller.java
 * 版权：Copyright 2018-2019 Love. Co. Ltd. All Rights Reserved.
 * 描述：Anthony.CHONG
 * 修改人：Anthony.CHONG
 * 修改时间：2020年03月23日
 * 系统名称：xiaole
 */
package com.sinbader.xiaole.admin.controller;

import cn.stylefeng.guns.sys.core.util.UserKit;
import cn.stylefeng.guns.sys.modular.enums.UserTypes;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sinbader.xiaole.common.enums.Status_;
import com.sinbader.xiaole.common.enums.Types_;
import com.sinbader.xiaole.common.service.impl.BaseSelectOptionSearchService;
import com.sinbader.xiaole.common.model.xiaolevo.SelectOptionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <页面初始化下拉框数据 ajax 异步加载>
 * <功能详细描述>
 *
 * @author CHONG
 * @version 1.0 2020年03月23日
 * @see BaseSelectOptionSearchContrller
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/baseCommonSelect")
public class BaseSelectOptionSearchContrller {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseSelectOptionSearchContrller.class);


    /**
     * baseSelectOptionSearchService
     */
    @Autowired
    private BaseSelectOptionSearchService baseSelectOptionSearchService;

    /**
     * 初始化-下拉框
     */
    @RequestMapping(value = "/initStatusOrTypes")
    @ResponseBody
    public JSONArray initSalesStatus(HttpServletRequest request) {
        String enumCode = request.getParameter("enumCode");
        JSONArray dataArray = new JSONArray();
        switch (enumCode) {
            case "SalesStatus"://销售状态
                Status_.SalesStatus[] values = Status_.SalesStatus.values();
                if (values.length != 0) {
                    for (Status_.SalesStatus value : values) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "CountType"://销售状态
                Types_.CountType[] countTypes = Types_.CountType.values();
                if (countTypes.length != 0) {
                    for (Types_.CountType value : countTypes) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "MoveType"://移货类型
                Types_.MoveType[] moveTypes = Types_.MoveType.values();
                if (moveTypes.length != 0) {
                    for (Types_.MoveType value : moveTypes) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "WarehouseType"://仓库类型
                Types_.WarehouseType[] warehouseTypes = Types_.WarehouseType.values();
                if (warehouseTypes.length != 0) {
                    for (Types_.WarehouseType value : warehouseTypes) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "ProcurementStatus"://采购状态
                Status_.ProcurementStatus[] procurementStatuses = Status_.ProcurementStatus.values();
                if (procurementStatuses.length != 0) {
                    for (Status_.ProcurementStatus value : procurementStatuses) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "SharedType"://是否分享
                Types_.SharedType[] sharedTypes = Types_.SharedType.values();
                if (sharedTypes.length != 0) {
                    for (Types_.SharedType value : sharedTypes) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            case "Status"://启用状态
                Status_.Status[] statuses = Status_.Status.values();
                if (statuses.length != 0) {
                    for (Status_.Status value : statuses) {
                        JSONObject item = new JSONObject();
                        item.put("id", value.getId());
                        item.put("value", value.getName());
                        dataArray.add(item);
                    }
                    break;
                }
            default:
                break;
        }
        return dataArray;

    }

    /**
     * 初始化-仓库下拉框
     */
    @RequestMapping(value = "/initWarehouseInfos")
    @ResponseBody
    public JSONArray initWarehouseInfos(HttpServletRequest request) {
        Map<String, Object> parameterMap = new HashMap<>();
        JSONArray dataArray = new JSONArray();
        LOGGER.info("查询仓库管理基础数据");
        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getSelectOptionsForWarehouse(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

    /**
     * 初始化-货架下拉框
     */
    @RequestMapping(value = "/initShelfSelectOptions")
    @ResponseBody
    public JSONArray initShelfSelectOptions(HttpServletRequest request) {
        Map<String, Object> parameterMap = new HashMap<>();
        //仓库id ，一般这个参数一定不会为空，需要指定这个参数，从而获取到 某个仓库下的 货架信息集合
        parameterMap.put("warehouseId",request.getParameter("warehouseId"));
        JSONArray dataArray = new JSONArray();
        LOGGER.info("查询货架信息初始化下拉框");
        List<SelectOptionDto> selectOptionDtoList =
                baseSelectOptionSearchService.initShelfSelectOptions(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

    /**
     * 异步加载 - 厂家信息 初始化 下拉框
     */
    @RequestMapping(value = "/getFactoryNumList")
    @ResponseBody
    public JSONArray getFactoryNumList(@RequestParam(value = "q", required = false) String q,
                                       HttpServletRequest request) {
        String orgId = request.getParameter("orgId");
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("orgId",orgId);
        JSONArray dataArray = new JSONArray();
        LOGGER.info("查询供应商信息初始化下拉框");
        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getSelectOptionsSupplier(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

    /**
     * 异步加载 - 商品信息 初始化 下拉框
     */
    @RequestMapping(value = "/getProductList")
    @ResponseBody
    public JSONArray getProductList(@RequestParam(value = "q", required = false) String q,
                                    HttpServletRequest request) {
        String supplierId = request.getParameter("supplierId");
        String productId = request.getParameter("productId");
        String shopId = request.getParameter("shopId");
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("supplierId",supplierId);
        parameterMap.put("productId",productId);
        parameterMap.put("shopId",shopId);
        JSONArray dataArray = new JSONArray();
        List<SelectOptionDto> selectOptionDtoList =
                baseSelectOptionSearchService.getProductList(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }


    /**
     * 异步加载 - 商品分类 初始化 下拉框
     */
    @RequestMapping(value = "/getProductCategory")
    @ResponseBody
    public JSONArray getProductCategory(@RequestParam(value = "q", required = false) String q,
                                       HttpServletRequest request) {

        Map<String, Object> parameterMap = new HashMap<>();
        JSONArray dataArray = new JSONArray();
        LOGGER.info("查询商品分类信息初始化下拉框");
        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getProductCategory(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

    /**
     * 异步加载 - 门店信息 初始化 下拉框
     */
    @RequestMapping(value = "/getShopList")
    @ResponseBody
    public JSONArray getShopList(HttpServletRequest request) {
        String orgId = request.getParameter("orgId");
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("orgId",orgId);
        JSONArray dataArray = new JSONArray();
        LOGGER.info("查询门店信息初始化下拉框");
        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getShopList(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

    /**
     * 异步加载 - 门店信息 初始化 下拉框
     */
    @RequestMapping(value = "/getShopListByShopId")
    @ResponseBody
    public Object getShopListByShopId(HttpServletRequest request) {
        String loginOrgId = null;
        User user = UserKit.getUser();
        if (user.getType().equals(UserTypes.Shop)){
            loginOrgId = request.getParameter("loginOrgId");
        }
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("orgId",loginOrgId);
        List dataArray = new LinkedList();
        LOGGER.info("查询门店信息初始化下拉框");
        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getShopList(parameterMap);
        for (SelectOptionDto value : selectOptionDtoList) {
            JSONObject item = new JSONObject();
            item.put("id", value.getId());
            item.put("value", value.getName());
            dataArray.add(item);
        }
        return dataArray;
    }

//    /**
//     * 异步加载 - 厂家和门店信息 初始化 下拉框
//     */
//    @RequestMapping(value = "/getFactoryAndShopList")
//    @ResponseBody
//    public JSONArray getFactoryAndShopList(@RequestParam(value = "q", required = false) String q,
//                                       HttpServletRequest request) {
//        String orgId = request.getParameter("orgId");
//        Map<String, Object> parameterMap = new HashMap<>();
//        parameterMap.put("orgId",orgId);
//        JSONArray dataArray = new JSONArray();
//        LOGGER.info("查询供应商信息初始化下拉框");
//        List<SelectOptionDto> selectOptionDtoList = baseSelectOptionSearchService.getFactoryAndShopList(parameterMap);
//        for (SelectOptionDto value : selectOptionDtoList) {
//            JSONObject item = new JSONObject();
//            item.put("id", value.getId());
//            item.put("value", value.getName());
//            dataArray.add(item);
//        }
//        return dataArray;
//    }

}